/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

//===========================================================================================
#include <dylab/utils/log/Log.h>
#include <dylab/utils/log/File_LogListener.h>
#include <dylab/utils/log/Console_LogListener.h>

using namespace dylab;

//===========================================================================================
#include "TetrahedronGeometry_Tests.h"
//===========================================================================================
real_t randReal()
{
    return DYLAB_R(10) * (real_t)rand()/(real_t)rand();
}
//-----------------------------------------------------------------------------
void runTests()
{ DYLAB_TRACE_CALLS

    // initialize random number generator
    srand((unsigned int)time(NULL));

    // run 10 tests
    for (uint32_t i = 0; i < 200; i++)
    {
        // generate random tetrahedron
        TetrahedronGeometry tetraGeom;
        do 
        {
            for (TetrahedronGeometry::VertexIdxIterator vertexIdx; vertexIdx.isValid(); vertexIdx++)
            {
                tetraGeom[vertexIdx].x() = randReal();
                tetraGeom[vertexIdx].y() = randReal();
                tetraGeom[vertexIdx].z() = randReal();
            }
        }
        while (tetraGeom.isDegenerated());

        // run the tetrahedron tests
        runTetrahedronTests(tetraGeom);

        DYLAB_LOG("");
        DYLAB_LOG("//===========================================================================================");
        DYLAB_LOG("//===========================================================================================");
        DYLAB_LOG("");

    }
 }

//===========================================================================================
#ifdef DYLAB_PLATFORM_WINDOWS     
    #include <windows.h>
    INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) 
#else               
    int main(int argc, char **argv) 
#endif 
{
    // create dylab log
    Log * log = new Log();    

    try
    {   
        // register log listeners
        log->registerListener("file", new File_LogListener("dylab.log"));
        log->registerListener("console", new Console_LogListener(new Console("DYLAB console")));

        // run the geometry tests
        runTests();
    }
    catch (Exception & e) {
        DYLAB_LOG(e.getVerboseInfo());

        #ifdef DYLAB_PLATFORM_WINDOWS
            MessageBox(NULL, e.getVerboseInfo().c_str(), "An exception has occured", MB_OK | MB_ICONERROR | MB_TASKMODAL);
        #else
            std::cerr << "ExceptionBase occured:\n";
            std::cerr << e.getVerboseInfo() << "\n";
        #endif

        // abnormal program termination
        return 1;
    }
    catch (std::exception & e) { 
        DYLAB_LOG(e.what());

        #ifdef DYLAB_PLATFORM_WINDOWS
            String msg = typeid(e).name();
            msg += ":\n";
            msg += e.what();
            MessageBox(NULL, msg.c_str(), "An exception has occured", MB_OK | MB_ICONERROR | MB_TASKMODAL);
        #else
            std::cerr << "ExceptionBase occured (" << typeid(e).name() << "):\n";
            std::cerr << e.what() << "\n";
        #endif

        // abnormal program termination
        return 1;
    }
 
	// delete our log listeners
	delete log->unregisterListener("console");
	delete log->unregisterListener("file");	

    // delete dylab log
    delete log;

    // normal program termination
    return 0;
}